/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package photoshare.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import photoshare.dto.RoleDTO;
import photoshare.util.DBUtil;

/**
 *
 * @author PhotoShare
 */
public class RoleDAO {


    private String READ_BY_ALL = "SELECT * FROM ROLES";
    private String READ_BY_ID = "SELECT * FROM ROLES WHERE RoleID = ?";
    private String CREATE = "INSERT INTO ROLES VALUES(?,?,?)";
    private String DELETE_BY_ID = "UPDATE ROLES SET IsACTIVE= 0 WHERE RoleID = ?";
    private String UPDATE_BY_ID = "UPDATE ROLES SET PASS = ? WHERE RoleID = ?";

    private Connection conn = null;
    private PreparedStatement pst = null;


    public RoleDAO() {
    }

    public boolean create(RoleDTO roleDTO) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CREATE);
            pst.setString(1, roleDTO.getRoleName());
            pst.setString(2, roleDTO.getDescription());
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public ArrayList<RoleDTO> readAll() {
        try {
            ArrayList<RoleDTO> arr = null;
            arr = new ArrayList<RoleDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                RoleDTO dto = new RoleDTO();
                dto.setRoleName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public RoleDTO readById(Integer roleId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ID);
            pst.setInt(1, roleId);
            //pst = conn.prepareStatement("select * from acc WHERE accountID = 'admin'");
            ResultSet rs = pst.executeQuery();
            RoleDTO roleDTO = new RoleDTO();
            while (rs.next()) {
                roleDTO.setRoleName(rs.getString(2));
                roleDTO.setDescription(rs.getString(3));
            }
            return roleDTO;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public boolean updateById(RoleDTO roleDTO, Integer roleId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(UPDATE_BY_ID);
            //Tham so can Update trong cau lenh SQL
            pst.setString(1, roleDTO.getRoleName());
            //Tham so dieu kien cua cau lenh Update
            pst.setInt(2, roleId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public boolean deleteById(Integer roleId) throws SQLException {

        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(DELETE_BY_ID);
            //Tham so dieu kien cho cau lenh Update
            pst.setInt(1, roleId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }


}
